//
//  LGShotrTableViewController.m
//  dw
//
//  Created by Apple on 15/12/7.
//  Copyright © 2015年 Apple. All rights reserved.
//

#import "LGShotrTableViewController.h"
#import "LGShotr.h"
#import "LGShotrCell.h"
#import "LGshotrDetailController.h"
@interface LGShotrTableViewController ()
@property (nonatomic , strong) NSMutableArray *shotrs;
@property (nonatomic , assign) NSInteger page;

@end

@implementation LGShotrTableViewController
/** ShotrCell */
static NSString *ID  = @"ShotrCell";

/** 有数据时候刷新 */
- (void)setShotrs:(NSMutableArray *)shotrs {
    _shotrs = shotrs;
    [self.tableView reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // 注册cell
    [self.tableView registerClass:[LGShotrCell class] forCellReuseIdentifier:ID];
    // 直接刷新
    [self loadData];
   
    // 下拉刷新
    self.refreshControl = [[UIRefreshControl alloc] init];
    [self.refreshControl addTarget:self action:@selector(loadData) forControlEvents:UIControlEventValueChanged];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

// 下拉刷新
- (void)loadData {
    // 加载数据
    [LGShotr shotrListWithSuccessBlock:^(NSArray *array) {
        self.shotrs = [NSMutableArray arrayWithArray:array];
        self.page = 1;
    } errorBlock:^(NSError *error) {
        NSLog(@"%@",error);
    }];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [self.refreshControl endRefreshing];
    });

}



#pragma -mark /***************** 数据源方法 *****************/

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.shotrs.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   
    LGShotrCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[LGShotrCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    // 取出数据
    LGShotr *shotr = self.shotrs[indexPath.row];
    cell.shotrModel = shotr;
    
    return cell;
    
}

#pragma -mark /***************** 代理方法 *****************/
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    LGshotrDetailController *vc = [[LGshotrDetailController alloc] init];
    vc.shotrModel = self.shotrs[indexPath.row];
    
    [self.navigationController pushViewController:vc animated:YES];
    
    // 选中后 取消
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 350;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView.contentOffset.y > (self.shotrs.count - 2) * 350 - 64) {
        NSMutableDictionary *parameter = [[NSMutableDictionary alloc] init];
        parameter.dictionary =
        @{@"Appid":@"10000024",
          @"Appkey":@"824b2a4e7ca1b804c6a1a0537c0bc212",
          @"act":@"lists",
          @"client_ver":@"1.2.1",
          @"mid":@"e553e2d24021fb8a156296b27e95fe3e4cf40791",
          //@"page":@"2",
          @"platform":@"iPhone9.1",
          @"yd_auth":@"d4f6ZD7td1cA6eH0SFATxSF07DKOXB%2B7gHKhfLtmV8FYgoK%2BUm4Wp%2FCaZti6tWV75B%2FtEtCn12jixPPuZvdHoYAe%2FFntpIYn2dLpN89taV3K1vhT3Q%2BBDBixLRwmKZwel%2BXn",
          @"yd_key":@"9563klroXuyAUEc61OJWViLHGR%2BCzHgbhYmyhwkZYSJ3",
          @"yd_uid":@"7014"};
        self.page++;
        [parameter setObject:[NSNumber numberWithInteger:self.page] forKey:@"page"];
        [[LGNetworkTools sharedManager] POST:@"/short" parameters:parameter constructingBodyWithBlock:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
            NSArray *array = responseObject[@"data"];
            
            [array enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                LGShotr *shotr = [LGShotr shotrWithDict:obj];
                [self.shotrs addObject:shotr];
                
                [self.tableView reloadData];
                
            }];
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            NSLog(@"错误%@",error);
        }];
    }
}

@end
